import dateUtil from '../../services/_utils/date';
import chatApi from '../../services/chat/chat';

Page({
  data: {
    currentUserId: null,
    conversionList: [],
    searchKey: ''
  },

  onLoad() {},

  onShow() {
    // 获取会话信息
    chatApi.getConversions().then(res => {
      console.log(res)
      res.forEach(o => {
        o.createTime = dateUtil.formatDateTime(o.createTime)
      })
      this.setData({
        conversionList: res
      })
    });

    this.setData({
      currentUserId: getApp().globalData.userInfo.userId,
    })
    this.getTabBar().init();
  },
  onPullDownRefresh() {},

  toChat(e) {
    console.log(e.currentTarget.dataset)
    const groudId = e.currentTarget.dataset.groupid;
    let toUserId = null
    let toUserIcon = null
    this.data.conversionList.forEach(o => {
      if (o.groupId == groudId) {
        if (this.data.currentUserId == o.fromUser) {
          toUserId = o.toUser;
          toUserIcon = o.toUserAvatarUrl
        } else {
          toUserId = o.fromUser;
          toUserIcon = o.fromUserAvatarUrl
        }
      }
    })
    console.log(groudId, toUserId, toUserIcon)
    wx.navigateTo({
      url: '/pages/chat/chatRoom/chatRoom?groupId=' + groudId + '&userId=' + toUserId + '&toUserIcon=' + toUserIcon
    });
  },
  inputting: function (e) {
    console.log(e);
    this.setData({
      searchKey: e.detail
    });
  },
  stringContains(str, searchStr) {
    console.log(1212)
    return str.indexOf(searchStr) !== -1;
  }
});